from .base_command import BaseCommand
from cleo.helpers import argument, option
from tjob.monitor import PaiMonitor
from tjob.meta import JobManger


class MonitorCommand(BaseCommand):
    name = "monitor"
    description = "Monitor submitted job and send notifications in Feishu"
    arguments = [
        argument(
            "names",
            description="Monitor which job names? Support list or str",
            multiple=True,
        )
    ]

    # Query interval time
    options = [
        option(
            "query_interval_time",
            "i",
            flag=False,
            description="query job status every query_interval_time seconds query once",  # noqa
            default=60,
        ),
        option(
            "user_list",
            "u",
            flag=False,
            description="User[feishu] who will recipe the final job msg of "
            + "monitored job/jobs, split by ',', e.g. 'dechuangyou, user1, user2' ",
            default="",
        ),
        option(
            "max_round",
            flag=False,
            description="monitor max round",
            default=10,
        ),
    ]

    def handle(self) -> int:
        job_ids = [JobManger().fetch_id(name) for name in self.argument("names")]
        pai_monitor = PaiMonitor(
            job_ids,
            self.option("user_list"),
            int(self.option("query_interval_time")),
            int(self.option("max_round")),
        )
        pai_monitor.monitor()
